package com.charles.config;

import com.charles.event.listener.ExportEventListener;
import com.charles.service.DefaultExportRecordHandler;
import com.charles.service.ExportRecordHandler;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

/**
 * @author charles
 * @date 2023/5/14 22:06
 */
@EnableConfigurationProperties(ExportProperties.class)
@MapperScan("com.charles.mapper")
@EnableAsync
public class ExportAutoConfiguration {

    @Bean
    @ConditionalOnMissingBean
    public ExportEventListener exportEventListener() {
        return new ExportEventListener();
    }

    @Bean
    public ThreadPoolTaskExecutor exportThreadPool() {
        ThreadPoolTaskExecutor threadPoolTaskExecutor = new ThreadPoolTaskExecutor();
        threadPoolTaskExecutor.setThreadNamePrefix("export-thread-");
        threadPoolTaskExecutor.setCorePoolSize(20);
        threadPoolTaskExecutor.setMaxPoolSize(100);
        threadPoolTaskExecutor.setQueueCapacity(50);
        return threadPoolTaskExecutor;
    }

    @Bean
    @ConditionalOnMissingBean
    public ExportRecordHandler exportRecordHandler() {
        return new DefaultExportRecordHandler();
    }
}
